﻿//
// ToDoList v2.0
// Onat Yigit Mercan
// onatmercan [at] gmail [dot] com
//
using System;
using System.Windows;

namespace ToDoList
{
    /// <summary>
    /// Interaction logic for ConfigurationWindow.xaml
    /// </summary>
    public partial class ConfigurationWindow : Window
    {
        private string strKey = @"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
        private string strValue = "ToDo List";
        private RegistryHandler registryHandler;
        private IntPtr ownerHandle;
        private FileHandler fileHandler;

        public ConfigurationWindow(IntPtr ownerHandle, FileHandler fileHandler)
        {
            this.ownerHandle = ownerHandle;
            this.fileHandler = fileHandler;
            InitializeComponent();
        }

        #region event handlers
        //
        // Event handlers
        //
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                registryHandler = new RegistryHandler(strKey, strValue);
                chkStartUp.IsChecked = registryHandler.CheckRegistry();
                chkAlwayOnTop.IsChecked = Properties.Settings.Default.InFront;
                if (!Properties.Settings.Default.IsCustomPath)
                    textBlockPath.Text = Environment.GetFolderPath(
                        Environment.SpecialFolder.MyDocuments);
                else
                    textBlockPath.Text = Properties.Settings.Default.directoryPath;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,
                    "ToDo List", MessageBoxButton.OK,
                    MessageBoxImage.Error);
            }
        }
        private void textBlockPath_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            try
            {
                System.Windows.Forms.FolderBrowserDialog folderBrowserPath = new System.Windows.Forms.FolderBrowserDialog();

                if (folderBrowserPath.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string oldFilePath = fileHandler.FilePath;
                    textBlockPath.Text = folderBrowserPath.SelectedPath;
                    Properties.Settings.Default.directoryPath = folderBrowserPath.SelectedPath;
                    Properties.Settings.Default.IsCustomPath = true;
                    fileHandler.DirectoryPath = folderBrowserPath.SelectedPath;
                    fileHandler.SetFilePath();
                    if (!fileHandler.CheckFile())
                        fileHandler.MoveFile(oldFilePath, fileHandler.FilePath);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,
                    "ToDo List", MessageBoxButton.OK,
                    MessageBoxImage.Error);
            }
        }
        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                bool startUp = chkStartUp.IsChecked.Value;
                registryHandler.ChangeRegistry(startUp);
                Properties.Settings.Default.InFront = chkAlwayOnTop.IsChecked.Value;
                Properties.Settings.Default.Save();
                if (Properties.Settings.Default.InFront)
                    Win32.SetWindowInFront(ownerHandle);
                else
                    Win32.SetWindowInBack(ownerHandle);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,
                    "ToDo List", MessageBoxButton.OK,
                    MessageBoxImage.Error);
            }
            this.Close();
        }
        #endregion
    }
}